package com.liyuan.yiqixing.controller;

import com.liyuan.yiqixing.model.Item;
import com.liyuan.yiqixing.model.ResultJson;
import com.liyuan.yiqixing.model.Signup;
import com.liyuan.yiqixing.model.User;
import com.liyuan.yiqixing.service.ItemService;
import com.liyuan.yiqixing.service.SignupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class ItemController {
    @Autowired
    ItemService itemService;
    @Autowired
    SignupService signUpService;
    @PostMapping("/item/{itemId}")
    public ResultJson<Object> item(@PathVariable("itemId")Integer id){
        Item item=itemService.selectItem(id);
        return new ResultJson<>(200,"get item->"+id+" success",item);
    }

    /**
     * 报名活动/义宿
     * @return
     */
    @PostMapping("/item/signup")
    public ResultJson<Object> signup(@RequestBody Signup signup){
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer signupId = signUpService.checkIsAlreadySignup(principal.getId(),signup.getItemId());
        if(signupId!=null){
            return new ResultJson<>(-1,"already signed up!",null);
        }
        signUpService.signup(principal.getId(),signup.getItemId(),signup.getHostelId());
        return new ResultJson<>(200,"sign up success!",null);
    }
}
